package ge.telasi.tasks.ui.task;

import ge.telasi.tasks.model.Task;
import ge.telasi.tasks.model.TaskRelation;
import ge.telasi.tasks.ui.comp.TableModel;
import java.util.ArrayList;
import java.util.List;

/**
 * @author dimitri
 */
public class TaskRelationTable extends TaskTable {

    public List<TaskRelation> getRelations(boolean asParent) {
        List<Task> tasks = getFullData();
        List<TaskRelation> relations = new ArrayList<TaskRelation>();
        if (tasks != null) {
            for (Task task : tasks) {
                TaskRelation relation = new TaskRelation();
                if (asParent) {
                    relation.setParent(task);
                } else {
                    relation.setChild(task);
                }
                relations.add(relation);
            }
        }
        return relations;
    }

    public void setRelations(List<TaskRelation> relations, boolean asParent) {
        TableModel<Task> model = (TableModel<Task>) getModel();
        for (TaskRelation relation : relations) {
            Task task = asParent ? relation.getParent() : relation.getChild();
            if (task != null) {
                List<Task> data = model.getData();
                if(data == null || !data.contains(task)) {
                    model.addItem(task);
                }
            }
        }
    }

}
